package com.sangeng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sangeng.constants.SystemConstants;
import com.sangeng.domain.ResponseResult;
import com.sangeng.domain.entity.Link;
import com.sangeng.domain.vo.LinkVo;
import com.sangeng.domain.vo.PageVo;
import com.sangeng.mapper.LinkMapper;
import com.sangeng.service.LinkService;
import com.sangeng.utils.BeanCopyUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LinkServiceImpl extends ServiceImpl<LinkMapper, Link> implements LinkService {
    /**
     * 获取所有的友链（友情链接，本页页面的超链接）
     * @return
     */
    @Override
    public ResponseResult getAllLink() {
        //1.获取所有审核通过的友链
        LambdaQueryWrapper<Link> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Link::getStatus, SystemConstants.LINK_STATUS_SUCCESS);
        List<Link> list = list(lqw);//不需要进行分页查询
        //2.统一封装结果
        List<LinkVo> linkVos = BeanCopyUtils.copyBeanList(list, LinkVo.class);
        return ResponseResult.okResult(linkVos);
    }
    /**
     * 分页查询所有的友链
     * @param pageNum
     * @param pageSize
     * @param name
     * @param status
     * @return
     */
    @Override
    public ResponseResult<PageVo> getAllLinkAdmin(Integer pageNum, Integer pageSize,
                                                  String name, String status) {
        LambdaQueryWrapper<Link> lqw=new LambdaQueryWrapper<>();
        lqw.eq(status!=null,Link::getStatus,status);
        lqw.like(name!=null,Link::getName,name);
        Page<Link> page=new Page<>(pageNum,pageSize);
        page(page,lqw);
        return ResponseResult.okResult(new PageVo(page.getRecords(),page.getTotal()));
    }
    /**
     * 添加友链
     * @param link
     * @return
     */
    @Override
    public ResponseResult addLink(Link link) {
        save(link);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult<Link> getLinkById(Long id) {
        Link link = getById(id);
        return ResponseResult.okResult(link);
    }
    /**
     * 更新友链
     * @param link
     * @return
     */
    @Override
    public ResponseResult updateLink(Link link) {
        updateById(link);
        return ResponseResult.okResult();
    }
    /**
     * 根据id删除友链
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteLinkById(Long id) {
        removeById(id);
        return ResponseResult.okResult();
    }
}
